package com.xk.app_home.fragment

import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.LogUtils


import com.xk.app_home.viewmodel.HomeFragmentVM
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.OnClickItem
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader

import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.xk.app_home.adapter.ArticleAdapter
import com.xk.app_home.adapter.BannerAdapter
import com.xk.app_home.databinding.BlankFragmentBinding

@Route(path = "/app/mide")
class HomeFragment : Fragment() {

    private lateinit var viewModel: HomeFragmentVM
    private  var adapter: ArticleAdapter? = null
    private  var refreshLayout: RefreshLayout? = null
    private lateinit var viewPager2:ViewPager2
    private lateinit var badapter: BannerAdapter
    private var position : Int = 0
    var bind : BlankFragmentBinding? = null
    private val binding get() = bind!!
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        bind = BlankFragmentBinding.inflate(inflater,container,false)
        val view =binding.root
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewbind()
    }

    private fun setlisten(){
        refreshLayout!!.setOnRefreshListener { refreshLayout ->
            viewModel.getArticle(0,20)
        }
        refreshLayout!!.setOnLoadMoreListener { refreshLayout ->
            position = adapter!!.itemCount
            viewModel.getArticle(adapter!!.data.curPage+1,20)
        }
        viewPager2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
            }
        })
    }

    fun viewbind(){
        viewModel = ViewModelProvider(this).get(HomeFragmentVM::class.java)
        viewPager2 = bind!!.vpBanner
        refreshLayout = bind!!.refreshLayout
        refreshLayout!!.setRefreshHeader(ClassicsHeader(context))
        refreshLayout!!.setRefreshFooter(ClassicsFooter(context))
        setlisten()
        viewModel.dataResult.observe(viewLifecycleOwner, Observer {
            if(adapter == null){
                adapter = ArticleAdapter(it,object : OnClickItem {
                    override fun onclickItem(url: String) {
                        ARouter.getInstance().build("/app/article").withString("url",url)
                            .navigation()
                    }
                })
                bind!!.blankRy.adapter =adapter
            }else{
                adapter!!.data = it
                adapter!!.notifyDataSetChanged()
            }
            LogUtils.d("postion ==$position")
            refreshLayout?.finishRefresh()
            refreshLayout?.finishLoadMore()
        })
        viewModel.bannerResult.observe(viewLifecycleOwner, Observer {
            badapter = BannerAdapter(it, requireContext(),object : OnClickItem {
                override fun onclickItem(url: String) {
                    ARouter.getInstance().build("/app/article").withString("url",url).navigation()
                }
            })
            viewPager2.adapter = badapter
        })

        viewModel.getArticle(0,20)
        viewModel.getBanner()
    }


}